package cn.clj1.builtInFunctions;

import cn.clj1.MyClojureInterp;
import cn.clj1.cell.CljCell;
import cn.clj1.cell.CljCellType;
import cn.clj1.recur.CljRecur;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class CljDataType {

    /**
     * hash table
     * */
    public static CljCell Clj_list_hash_table(List<CljCell> lst, Hashtable<String, CljCell> funcHs, CljRecur cljRecur, CljCell rs) throws Exception {
        Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
        for (int i = 0; i < lst.size(); i += 2) {
            if (lst.get(i).getCljCellType() == CljCellType.key_symbol) {
                CljCell cljCell = MyClojureInterp.eval(lst.get(i + 1), funcHs, cljRecur, new CljCell());
                String key = lst.get(i).getCell().toString();
                hashtable.put(key, cljCell);
            }
        }
        rs.setCell(hashtable);
        rs.setCljCellType(CljCellType.hash_table);
        return rs;
    }

    /**
     * str
     * */
    public static CljCell Clj_list_str(CljCell cell, Hashtable<String, CljCell> funcHs, CljRecur cljRecur, CljCell rs) throws Exception {
        List<CljCell> lst = new ArrayList<>();
        for (CljCell c : (List<CljCell>)cell.getCell()) {
            lst.add(MyClojureInterp.eval(c, funcHs, cljRecur, rs));
        }
        rs.setCell(lst);
        rs.setCljCellType(CljCellType.list);
        return rs;
    }
}
